import Vue from 'vue'
import Router from 'vue-router'

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}
// const originalReplace = Router.prototype.replace
// Router.prototype.replace = function replace(location) {
//   return originalReplace.call(this, location).catch(err => err)
// }

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* 路由模块 */
import toolRouter from './modules/tool'

/**
 * 配置说明
 *
 * // 当设置 true 的时候该路由不会在侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * hidden: true // (默认 false)
 *
 * // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * redirect: 'noRedirect'
 *
 * // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式
 * // 只有一个时，会将那个子路由当做根路由显示在侧边栏
 * // 若你想不管路由下面的 children 声明的个数多少都显示你的根路由
 * // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * alwaysShow: true
 *
 * // 设定路由的名字，一定要填写且与vue文件name一致，不然使用<keep-alive>时会出现各种问题（不一致会导致没有缓存，会重新拉取数据）
 * name: 'Index'
 * meta: {
 *   routes: ['admin/Index/index'] // 设置该路由进入的权限（菜单链接），支持多个权限叠加
 *   title: '控制台' // 设置该路由在侧边栏和面包屑中展示的名字
 *   icon: 'el-icon-s-home' // 设置该路由的图标，element-ui 的 icon
 *   noCache: true // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
 *   breadcrumb: false //  如果设置为false，则不会在breadcrumb面包屑中显示(默认 true)
 *   affix: true // 若果设置为true，它则会固定在tags-view中(默认 false)
 *   // 当路由设置了该属性，则会高亮相对应的侧边栏。
 *   // 这在某些场景非常有用，比如：一个文章的列表页路由为：/article/list
 *   // 点击文章进入文章详情页，这时候路由为/article/1，但你想在侧边栏高亮文章列表的路由，就可以进行如下设置
 *   activeMenu: '/article/list'
 * }
 */

/**
 * constantRoutes
 * 不需要动态判断权限的路由，如登录、404、401等页面
 * 所有用户都可以访问
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/admin/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/admin/auth/login'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/admin/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/admin/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    redirect: '/dashboard',
    component: Layout,
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/admin/index/index'),
        meta: {
          title: '数据大屏',
          icon: 'el-icon-s-home',
          affix: true
        }
      }
    ]
  }
]

/**
 * asyncRoutes
 * 需要动态判断权限并通过 addRoutes 动态添加的页面
 * 需要分配相应权限才可以访问
 */
export const asyncRoutes = [
  /*
  {
    path: '/admin/admin_manage',
    //name: 'AdminAdminManage',
    redirect: 'noRedirect',
    alwaysShow: true,
    component: Layout,
    meta: {
      title: '用户管理',
      icon: 'el-icon-user',
      // routes: [
      //   'admin/admin/list',
      //   'admin/rbac/list',
      //   'admin/log/list',
      //   'admin/admin/profile'
      // ]
    },
    children: [
      {
        path: '/admin/admin_group',
        redirect: 'noRedirect',
        component: () => import('@/views/admin/nested/index'), // 嵌套
        meta: {
          title: '管理组',
          icon: '',
          // routes: [
          //   'admin/rbac/list',
          //   'admin/admin/list',
          //   'admin/admin/profile'
          // ]
        },
        children: [
          {
            path: '/admin/role',
            name: 'AdminRole',
            component: () => import('@/views/admin/role/list'),
            meta: {
              title: '角色管理',
              //routes: ['admin/role/list']
            }
          },
          {
            path: '/admin/admin',
            name: 'AdminAdmin',
            component: () => import('@/views/admin/admin/index'),
            meta: {
              title: '管理员',
              routes: [
                //'admin/admin/list',
                //'admin/admin/profile'
              ]
            }
          },
          {
            path: '/admin/profile',
            name: 'AdminProfile',
            component: () => import('@/views/admin/admin/profile'),
            meta: {
              title: '个人中心',
              //routes: ['admin/admin/profile']
            }
          },
          {
            path: '/admin/resetPwd',
            name: 'AdminResetPwd',
            component: () => import('@/views/admin/admin/reset-pwd'),
            meta: {
              title: '修改密码',
              //breadcrumb: false,
              //routes: ['admin/admin/resetPwd']
            },
            hidden: true
          }
        ]
      },
      {
        path: '/admin/log',
        name: 'AdminLog',
        component: () => import('@/views/admin/log/list'),
        meta: {
          title: '日志管理',
          //routes: ['admin/log/list']
        }
      }
    ]
  },
  */

  /** when your routing map is too long, you can split it into small modules **/
  // toolRouter,

  // 404页面必须放在最后!!!
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]

export const routerMap = {
  // login: require('login/index').default // 同步的方式
  // login: () => import('login/index') // 异步的方式
  layout: Layout,
  nested: () => import('@/views/admin/nested/index'),
  empty: () => import('@/views/admin/index/empty'),
  default: () => import('@/views/admin/index/index'),
  adminrole: () => import('@/views/admin/role/list'),
  articlecate: () => import('@/views/admin/category/list'),
  agreementcate: () => import('@/views/admin/category/agreementcatelist'),
  agreementnew: () => import('@/views/admin/category/agreementnewlist'),
  articlecate2: () => import('@/views/admin/category/list'),
  article: () => import('@/views/admin/article/list'),
  article_add: () => import('@/views/admin/article/create'),
  article_edit: () => import('@/views/admin/article/edit'),
  adminmenu: () => import('@/views/admin/menu/list'),
  region: () => import('@/views/admin/region/list'),
  file: () => import('@/views/admin/file/list'),
  systemfile: () => import('@/views/admin/system-file/index'),
  database_export: () => import('@/views/admin/database/export'),
  database_import: () => import('@/views/admin/database/import'),
  setting: () => import('@/views/admin/setting/index'),
  kefu: () => import('@/views/admin/kefu/index'),//客服
  dictionary: () => import('@/views/admin/dictionary/index'),
  dictionary_subdictionary: () => import('@/views/admin/dictionary/subdictionary'),
  carousel: () => import('@/views/admin/carousel/index'),
  diamond: () => import('@/views/admin/diamond/index'),//金刚区
  activateimg: () => import('@/views/admin/activateimg/index'),//启动图
  freight: () => import('@/views/admin/freight/index'),
  freight_save: () => import('@/views/admin/freight/save'),
  affiche: () => import('@/views/admin/affiche/index'),
  affiche_admin: () => import('@/views/admin/affiche_admin/index'),
  smsconfig: () => import('@/views/admin/smsconfig/index'),
  agreement: () => import('@/views/agreement/index'),
  agreement_save: () => import('@/views/agreement/save'),
  version: () => import('@/views/admin/version/index'),

  // 用户模块
  admin: () => import('@/views/user/admin/index'),
  consumer: () => import('@/views/user/consumer/index'),
  consumer_add: () => import('@/views/user/consumer/add'),
  consumer_configuration: () => import('@/views/user/consumer/configuration'),
  admin_profile: () => import('@/views/user/admin/profile'),
  admin_resetpwd: () => import('@/views/user/admin/reset-pwd'),
  adminlog: () => import('@/views/user/log/list'),
  member: () => import('@/views/user/member/list'),
  member_chart: () => import('@/views/user/member/chart'),
  grouping: () => import('@/views/user/grouping/index'),
  label: () => import('@/views/user/label/index'),
  grade: () => import('@/views/user/grade/index'),
  configuration: () => import('@/views/user/configuration/index'),
  payvip: () => import('@/views/user/payVip/index'),
  equity: () => import('@/views/user/payVip/equity'),
  // agreement: () => import('@/views/user/payVip/agreement'),
  // 订单模块
  orderlist: () => import('@/views/order/orderlist/index'),
  orderlist_supplierafter: () => import('@/views/order/supplierafter/index'),
  platformorder: () => import('@/views/order/platformOrder/index'),
  platformorder2: () => import('@/views/order/platformOrder2/index'),
  platformorder_aftersales: () => import('@/views/order/aftersales/index'),

  configs: () => import('@/views/order/config/index'),
  order: () => import('@/views/merchant/order/index'),
  order_afterpurchase: () => import('@/views/order/afterpurchase/index'),

  // 商品模块
  goods: () => import('@/views/commodity/goods/index'),
  goods_add: () => import('@/views/commodity/goods/add'),
  goodstype: () => import('@/views/commodity/goodstype/index'),
  shippingaddress: () => import('@/views/commodity/shippingaddress/index'),
  // 供应商模块
  application: () => import('@/views/supplier/supplierlist/application'),
  supplier_settlein: () => import('@/views/supplier/settlein/index'),
  supplierlist: () => import('@/views/supplier/supplierlist/index'),
  supplierlist_add: () => import('@/views/supplier/supplierlist/add'),
  supplierlevel: () => import('@/views/supplier/supplierlevel/index'),
  supplierorder: () => import('@/views/supplier/supplierorder/index'),
  supplierorder_add: () => import('@/views/supplier/supplierorder/add'),
  supplierorder_edits: () => import('@/views/supplier/supplierorder/edits'),
  specifications: () => import('@/views/supplier/specifications/index'),
  supplierorder_type: () => import('@/views/supplier/supplierorder/type'),
  suppliergoods: () => import('@/views/supplier/suppliergoods/index'),
  address: () => import('@/views/supplier/address/index'),
  delivery: () => import('@/views/supplier/delivery/index'),
  delivery_usershipment: () => import('@/views/supplier/delivery/usershipment'),
  delivery_merchantshipment: () => import('@/views/supplier/delivery/merchantshipment'),
  aftersales: () => import('@/views/supplier/aftersales/index'),
  aftersales_merchantaftersalesservice: () => import('@/views/supplier/aftersales/merchantaftersalesservice'),
  aftersales_useraftersalesservice: () => import('@/views/supplier/aftersales/useraftersalesservice'),
  // 商户模块
  tabulation: () => import('@/views/merchant/tabulation/index'),
  classification: () => import('@/views/merchant/classification/index'),
  merchantapply: () => import('@/views/merchant/merchantapply/index'),

  selecgoods_add: () => import('@/views/merchant/selecgoods/add'),
  shopauth: () => import('@/views/merchant/shopauth/index'),

  salesorder: () => import('@/views/merchant/ordermodule/salesorder'), // 销售订单
  selfpickuporders: () => import('@/views/merchant/ordermodule/selfpickuporders'), // 自提订单
  purchasereturnorder: () => import('@/views/merchant/ordermodule/purchasereturnorder'), // 采购退货记录
  selectionandprocurementrecords: () => import('@/views/merchant/ordermodule/selectionandprocurementrecords'), // 采购记录
  commodity: () => import('@/views/merchant/commodity/index'), // 商户选品列表

  // 商户管理
  allocation: () => import('@/views/merchant/allocation/index'), // 商户配置-商户配置
  profession: () => import('@/views/merchant/profession/index'), // 商户配置-商户行业配置
  selecgoods: () => import('@/views/merchant/selecgoods/index'), // 商户配置-商户选品池配置

  applyof: () => import('@/views/merchant/applyOf/index'),
  procure: () => import('@/views/merchant/procure/index'),
  procure_afterpickup: () => import('@/views/order/afterpickup/index'),

  // 优惠券模块
  courtesycard: () => import('@/views/coupon/courtesycard/index'),
  record: () => import('@/views/coupon/record/index'),

//插件管理
productadd: () => import('@/views/plugins/productadd/index'),
distribution: () => import('@/views/plugins/distribution/index'),
purchase: () => import('@/views/plugins/purchase/index'),
  // 财务管理
  recharge: () => import('@/views/finance/recharge/index'),
  vipcard: () => import('@/views/finance/vipcard/index'),
  datascreen: () => import('@/views/finance/datascreen/index'),
  vipcard_save: () => import('@/views/finance/vipcard/save'),
  config: () => import('@/views/finance/config/index'),
  records: () => import('@/views/finance/records/index'),
  rechargeaudit: () => import('@/views/finance/rechargeaudit/index'),
  withdrawalaudit: () => import('@/views/finance/withdrawalaudit/index'),
  accountaudit: () => import('@/views/finance/accountaudit/index'),
  detail: () => import('@/views/finance/detail/index'),
  suppliersettment: () => import('@/views/finance/suppliersettment/index'),
  // 活动专区
  agriculture: () => import('@/views/activity/agriculture/index'),
  allreturn: () => import('@/views/activity/allreturn/index'),
  seckill: () => import('@/views/activity/seckill/index'),
  seckill_add: () => import('@/views/activity/seckill/add'),
  convert: () => import('@/views/activity/convert/index'),
  convert_add: () => import('@/views/activity/convert/add'),
  sendfoints: () => import('@/views/activity/sendfoints/index'),
  sendfoints_add: () => import('@/views/activity/sendfoints/add'),
  raffle: () => import('@/views/activity/raffle/index'),
  raffle_save: () => import('@/views/activity/raffle/save'),
  raffle_savegoods: () => import('@/views/activity/raffle/savegoods'),
  raffle_saverules: () => import('@/views/activity/raffle/saverules'),
  template: () => import('@/views/activity/raffle/template'),
  signin: () => import('@/views/activity/signin/index'),
  signin_saverules: () => import('@/views/activity/signin/saverules'),
  // demo: () => import('@/views/activity/demo/indexcopy'),
  demo: () => import('@/views/activity/demo/index'),
  pullactivity: () => import('@/views/activity/pullactivity/index'),
  //普通拉新
  recruitment: () => import('@/views/activity/recruitment/index'),
  //商品拉新
  shopprecruitment: () => import('@/views/activity/shopprecruitment/index'),
  //部门经理
  manager: () => import('@/views/activity/manager/index'),

  threedaysback: () => import('@/views/activity/threedaysback/index'),
  bridalgift: () => import('@/views/activity/bridalgift/index'),

  // 本地团
  groupbuyingmerchant: () => import('@/views/groupbuying/merchant/index'),
  groupbuyingmerchant_save: () => import('@/views/groupbuying/merchant/save'),
  groupbuyingclassification: () => import('@/views/groupbuying/classification/index'),
  groupbuyingselection: () => import('@/views/groupbuying/selection/index'),
  groupbuyingselection_save: () => import('@/views/groupbuying/selection/save'),
  groupbuyingverifier: () => import('@/views/groupbuying/verifier/index'),

  // 系统管理
  user: () => import('@/views/admin/user/index'),
  errorreporting: () => import('@/views/admin/errorreporting/index'),
  behavior: () => import('@/views/admin/behavior/index'),
  systems: () => import('@/views/admin/systems/index'),


  // 运营管理
  operationssalesorder: () => import('@/views/operations/salesorder/index'), // 销售订单
  operationsaftersalesorders: () => import('@/views/operations/aftersalesorders/index'), // 售后订单
  realnamex: () => import('@/views/realname/realnamex/index'), // 用户认证审核记录
  // 供应商角色模块
  rolebalance: () => import('@/views/supplierrole/balance/index'), // 结算记录
  roleproduct: () => import('@/views/supplierrole/product/index'), // 产品列表
  roleproduct_specifications: () => import('@/views/supplierrole/product/type'), // 规格管理
  roleproduct_eidt: () => import('@/views/supplierrole/product/edits'), // 编辑产品
  roleproduct_add: () => import('@/views/supplierrole/product/add'), // 添加商品
  receiptuserorders: () => import('@/views/supplierrole/receipt_order/user_orders'), // 用户待发货订单
  receiptmerchantorders: () => import('@/views/supplierrole/receipt_order/merchant_orders'), // 用户待发货订单
  afteruserorders: () => import('@/views/supplierrole/shipping_order/user_orders'), // 用户售后订单
  aftermerchantorders: () => import('@/views/supplierrole/shipping_order/merchant_orders') // 用户售后订单
}

const createRouter = () =>
  new Router({
    // browserHistory 路由模式
    // mode: 'history', // 去掉url中的#号，需要服务器支持
    scrollBehavior: () => ({
      y: 0
    }),
    routes: constantRoutes
  })

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
